package com.zhiche.lisa.lspm.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.weiXin.WeiXinExport;
import com.zhiche.lisa.lspm.service.weiXin.WeiXinExportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 微信使用状况导出 前端控制器
 * </p>
 *
 * @author zhangkun
 * @since 2018-10-24
 */
@RestController
@RequestMapping("/weixinExport")
@Api(value = "weixinExport", description = "统计微信公众号使用率情况", tags = "统计微信公众号使用率情况")
public class WeiXinExportController {
    public Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private WeiXinExportService weixinExportService;

    @PostMapping(value = "/queryUsageRate")
    @ApiOperation(value = "/queryUsageRate", notes = "查询统计公众号使用率", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = WeiXinExport.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<WeiXinExport>> queryUsageRate(@RequestBody Page page) {
        logger.info("WeiXinExportController/queryUsageRate data: {} ", page);
        RestfulResponse result = new RestfulResponse(0, "成功", null);
        try {
            Page statisticList = weixinExportService.queryWeChatPublicNumberUsageRate(page);
            result.setData(statisticList);
        } catch (BaseException be) {
            logger.error("WeiXinExportController/queryUsageRate error:{}", be.getMessage());
            result.setMessage(be.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            logger.error("WeiXinExportController/queryUsageRate error:{}", e.getMessage());
            result.setMessage("操作失败");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }


    @PostMapping(value = "/getWeChatPublicNumberUsageRate")
    @ApiOperation(value = "/getWeChatPublicNumberUsageRate", notes = "获取微信公众号使用率情况", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = WeiXinExport.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<WeiXinExport>> getWeChatPublicNumberUsageRate(@RequestBody String timeStamp) {
        logger.info("WeiXinExportController/insertERPRate data: {} ", timeStamp);
        RestfulResponse result = new RestfulResponse(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            weixinExportService.insertERPRate();
        } catch (BaseException e) {
            logger.error("WeiXinExportController.insertERPRate error: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            logger.error("WeiXinExportController.insertERPRate error: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        }
        return result;
    }

    @PostMapping(value = "queryWeChatPublicNumberUsageRate")
    @ApiOperation(value = "queryWeChatPublicNumberUsageRate-API", notes = "导出Excel前查询", response = RestfulResponse.class)
    public RestfulResponse<List<WeiXinExport>> queryWeChatPublicNumberUsageRate(@RequestBody Map<String, Object> condition) {
        logger.info("WeiXinExportController/queryWeChatPublicNumberUsageRate data: {}", condition);
        RestfulResponse<List<WeiXinExport>> result = new RestfulResponse<>(0, "成功", null);
        try {
            List<WeiXinExport> weiXinExportList = weixinExportService.queryWeChatPublicNumberUsageRate(condition);
            result.setData(weiXinExportList);
            return result;
        } catch (Exception e) {
            result.setCode(-1);
            result.setMessage(e.getMessage());
            return result;
        }
    }

}

